﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblMemberTypeDAL:ImtblMemberType
    {
        HKEA_CMSEntities model;
        mtblMemberType type;
        public mtblMemberTypeDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblMemberTypeDAL GetInstance()
        {
            return new mtblMemberTypeDAL();
        }

        public mtblMemberType GetEntity(int uid)
        {
            return model.mtblMemberTypes.Where(a => a.MemTypeUID == uid).SingleOrDefault();
        }

        public int InsertENT(mtblMemberType ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblMemberTypes.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblMemberType> GetListByENT(mtblMemberType ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblMemberTypes.Where(a => a.MemTypeUID > 0);
            if (ent.MemTypeUID > 0)
                result = result.Where(a => a.MemTypeUID == ent.MemTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblMemberType> GetListByENT(mtblMemberType ent)
        {
            var result = model.mtblMemberTypes.Where(a => a.MemTypeUID > 0);
            if (ent.MemTypeUID > 0)
                result = result.Where(a => a.MemTypeUID == ent.MemTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblMemberType ent)
        {
            var result = model.mtblMemberTypes.Where(a => a.MemTypeUID > 0);
            if (ent.MemTypeUID > 0)
                result = result.Where(a => a.MemTypeUID == ent.MemTypeUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblMemberType ent)
        {
            type = model.mtblMemberTypes.Where(a=>a.MemTypeUID==ent.MemTypeUID).FirstOrDefault();
            if (type != null)
            {
                type.Deleted = ent.Deleted;
                type.LastModifyBy = ent.LastModifyBy;
                type.LastModifyDate = DateTime.Now;
                type.MemType_Chi = ent.MemType_Chi;
                type.MemType_Eng = ent.MemType_Eng;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblMemberType ent)
        {
            type = model.mtblMemberTypes.Where(a => a.MemTypeUID == ent.MemTypeUID).FirstOrDefault();
            if (type != null)
            {
                model.mtblMemberTypes.DeleteObject(type);
            }
            return model.SaveChanges();
        }
    }
}
